import { createRouter, createWebHistory } from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import editorRoutes from './modules/editor'
import formRoutes from './modules/form'
import iframeRoutes from './modules/iframe'
import mapRoutes from './modules/map'
import microRoutes from './modules/micro'
import tableRoutes from './modules/table'
import vueFunRoutes from './modules/vue'
import othersRoutes from './modules/others'
// 权限路由
import permissionList from './router-permission'

import { getToken } from '@/utils'
import { useLoginStore } from '@/store/login'

const entryRoutes: IRouteInfo[] = [
  ...othersRoutes,
  ...vueFunRoutes,
  editorRoutes,
  formRoutes,
  iframeRoutes,
  mapRoutes,
  microRoutes,
  tableRoutes,
]

const CommonRoutes: IRouteInfo[] = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/login.vue'),
    meta: {
      name: 'sign_in',
    },
  },
]

export const DynamicRoutes: IRouteInfo[] = [
  {
    path: '',
    name: 'Layout',
    component: () => import('@/layout/index.vue'),
    meta: {
      name: 'home',
    },
    children: [
      ...entryRoutes,
      {
        path: 'panel',
        name: 'Panel',
        component: () => import('@/views/panel/index.vue'),
        meta: {
          name: 'panel',
          icon: 'tickets',
          subHide: true,
        },
      },
    ],
  },
]

const routes: IRouteInfo[] = [...DynamicRoutes, ...CommonRoutes]

const router = createRouter({
  history: createWebHistory(),
  routes: routes,
})

router.beforeEach(async (to: any, _from: any, next: any) => {
  NProgress.start()
  const loginStore = useLoginStore()
  const token = getToken()
  // console.log('...token', token)
  //未登录执行逻辑
  if (!token && to.path !== '/login') {
    return next({ path: '/login' })
  }
  //已登录执行逻辑
  if (to.path == '/login') {
    return next()
  }
  //重新加载动态路由
  if (!loginStore.permissionList.length && to.path != '/login') {
    const routerArr: IRouteInfo[] =
      window.localStorage.getItem('permission') == 'adminer'
        ? permissionList
        : []
    await loginStore.FETCH_PERMISSION(routerArr)
  }
  next()
})

router.afterEach((to: any, from: any, _next: any) => {
  const loginStore = useLoginStore()
  try {
    //设置标题
    if (to.meta.name) {
      document.title = to.meta.name + '_Vue3 TypeScript Vite Admin'
    }
  } catch (err) {}
  const arr = to.meta.hide ? [from, to] : to.matched
  let routerList: IRouteInfo[] = []
  arr.forEach((item: any) => {
    const obj: any = {
      name: item.name,
      meta: {
        name: item.meta.name,
      },
    }
    routerList.push(obj)
  })
  if (to.meta.hide) {
    //顶部面包屑
    loginStore.SET_CRUMB_LIST(routerList as [])
    //目前左边导航选中的active
    // store.commit("SET_CURRENT_MENU", to.name);
  } else {
    routerList = routerList.splice(1)
    //顶部面包屑
    loginStore.SET_CRUMB_LIST(routerList as [])
    //目前左边导航选中的active
    loginStore.SET_CURRENT_MENU(to.name)
  }
  NProgress.done()
})

export default router
